package com.zry.pda.ui.activity

import android.content.Intent
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
import com.zry.pda.R
import com.zry.pda.R.string.storeOrderList
import com.zry.pda.adapter.OutStockListAdapter
import com.zry.pda.adapter.StoreOrderListAdapter
import com.zry.pda.animation.RecyclerViewUtilKt
import com.zry.pda.been.LoginInfoBean
import com.zry.pda.been.OutStock
import com.zry.pda.been.StoreOrderBean
import com.zry.pda.presenter.HttpServer
import com.zry.pda.retrofit.ShowUserView
import com.zry.pda.ui.base.BaseActivity
import com.zry.pda.utils.ACacheUtil
import com.zry.pda.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_store_order_list.*
import kotlinx.android.synthetic.main.common_title.*

class StoreOrderListActivity : BaseActivity(), ShowUserView {

    private var storeOrderListAdapter: StoreOrderListAdapter? = null
    private var outStockListAdapter: OutStockListAdapter? = null
    private var storeOrderList: MutableList<StoreOrderBean>? = null
    private var outStockList: MutableList<OutStock>? = null

    private var title = ""
    private var pageStoreIndex = 1

    override fun getLayoutId(): Int {
        return R.layout.activity_store_order_list
    }

    override fun initData() {
        commonLeftLinearLayout.setOnClickListener { finish() }

        title = intent.getStringExtra("title")
        titleCenterText.text = title

        storeOrderList = mutableListOf()
        outStockList = mutableListOf()

        initRecycler()
    }

    private fun initRecycler() {
        recyclerViewUtilKt = RecyclerViewUtilKt(this, storeOrderRecycler)
        recyclerViewUtilKt?.initRecyclerView()

        storeOrderRecycler.useDefaultLoadMore() // 使用默认的加载更多的View。
        storeOrderRecycler.setLoadMoreListener(mLoadMoreListener) // 加载更多的监听。

        when (title) {
            "入库采集" -> {
                initStoreOrderAdapter()
                HttpServer(this).getStoreOrderList(ACacheUtil().getLoginInfo().EntID, pageStoreIndex, 10, this)
            }
            "出库采集" -> {
                initOutStockAdapter()
                HttpServer(this).getOutStockList(ACacheUtil().getLoginInfo().EntID, pageStoreIndex, 10, this)
            }
        }
    }


    private fun initStoreOrderAdapter() {
        storeOrderListAdapter = StoreOrderListAdapter(R.layout.item_store_order, storeOrderList)
        recyclerViewUtilKt?.setAdapter(storeOrderListAdapter!!)
        storeOrderListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, PutStorageActivity::class.java)
            intent.putExtra("StoreOrderBean", storeOrderList!![position])
            intent.putExtra("title", title)
            startActivity(intent)
        }
    }

    private fun initOutStockAdapter() {
        outStockListAdapter = OutStockListAdapter(R.layout.item_store_order, outStockList)
        recyclerViewUtilKt?.setAdapter(outStockListAdapter!!)
        outStockListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, OutTreasuryActivity::class.java)
            intent.putExtra("OutStock", outStockList!![position])
            intent.putExtra("title", title)
            startActivity(intent)
        }
    }


    private val mLoadMoreListener: SwipeMenuRecyclerView.LoadMoreListener = SwipeMenuRecyclerView.LoadMoreListener {
        storeOrderRecycler.postDelayed({
            when (title) {
                "入库采集" -> {
                    HttpServer(this).getStoreOrderList(ACacheUtil().getLoginInfo().EntID, pageStoreIndex, 10, this)
                }
                "出库采集" -> {
                    HttpServer(this).getOutStockList(ACacheUtil().getLoginInfo().EntID, pageStoreIndex, 10, this)
                }
            }
        }, 1000)
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            11 -> {
                val storeOrderBeanList: MutableList<StoreOrderBean> = data as MutableList<StoreOrderBean>
                if (pageStoreIndex == 1) {
                    if (storeOrderBeanList.size > 0) {
                        storeOrderRecycler.loadMoreFinish(false, true)
                        storeOrderList!!.addAll(storeOrderBeanList)
                        storeOrderListAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        storeOrderBeanList.size == 0 -> storeOrderRecycler.loadMoreFinish(true, false)
                        storeOrderBeanList.size > 0 -> {
                            pageStoreIndex++
                            storeOrderList!!.addAll(storeOrderBeanList)
                            storeOrderListAdapter?.notifyDataSetChanged()
                            storeOrderRecycler.loadMoreFinish(false, true)
                        }
                        else -> storeOrderRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
            16 -> {
                val outStockBeanList: MutableList<OutStock> = data as MutableList<OutStock>
                if (pageStoreIndex == 1) {
                    if (outStockBeanList.size > 0) {
                        storeOrderRecycler.loadMoreFinish(false, true)
                        outStockList!!.addAll(outStockBeanList)
                        storeOrderListAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        outStockBeanList.size == 0 -> storeOrderRecycler.loadMoreFinish(true, false)
                        outStockBeanList.size > 0 -> {
                            pageStoreIndex++
                            outStockList!!.addAll(outStockBeanList)
                            storeOrderListAdapter?.notifyDataSetChanged()
                            storeOrderRecycler.loadMoreFinish(false, true)
                        }
                        else -> storeOrderRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
        }
    }
}
